<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once('c_base.php');

class Users extends C_Base {

	/**
	 * 用户登录表单
	 * map to /users/<method_name>
	 */
	public function login()
	{            
		$this->lang->load('user');
		$data = array('lang'=> $this->lang->language);
		$this->load->view('inc_header',$data);
		$this->load->view('user_login',$data);
		$this->load->view('inc_footer');	
	}
	
	/**
	 * 提交登陆
	 * map to /users/do_login
	 */
	public function do_login()
	{            
		//载入类库
		$this->load->model('v_Message');
		$this->load->model('m_User');
		$this->lang->load('user');
		
		//处理登陆方法
		$username = $this->input->post('username',true);
		$password = $this->input->post('password',true);				
		$user = $this->m_User->login($username,$password);				
		
		//登陆结果处理	
		if($user){
			//消息返回
			$this->v_Message->message = $this->lang->line('login_success');	
			$this->v_Message->success = true;
			
			//记录SESSION
			$this->session->unset_userdata('logined_user');
			$user->password='';
			$this->session->set_userdata('logined_user',$user);	
			
			//写入日志
			$this->load->model('m_Log');
			$this->m_Log->write($user->user_id,$this->v_Message->message, $user->username,'LOGIN');
		}
		else{
			//返回错误
			$this->v_Message->message = $this->lang->line('login_fail');
		}
		
		//输出Json
		echo json_encode($this->v_Message);	
	}
	
	/**
	 * 用户退出
	 * map to /users/logout
	 */
	public function logout()
	{ 
		$this->session->unset_userdata('logined_user');
		$this->validate_login();
	}
	
	/**
	 * 用户列表
	 * map to /users/
	 */
	public function index()
	{            
		$this->validate_login();
	
		$this->lang->load('user');
		$this->load->model('m_User');
		
		$page = $this->uri->segment(3,0) * 1;
		if($page ==0)$page = 1;
		
		$pager = $this->m_User->get_pager($this->page_size);
		$lst = $this->m_User->select_list(null,null, $this->page_size, ($page-1) * $this->page_size);
		
		$page_html = $this->create_pager_render('/users/index/',$pager['total'],$this->page_size);
		
		$data = array('lang'=> $this->lang->language,'list'=>$lst,'pager'=>$page_html);
		$this->load->view('inc_header',$data);
		$this->load->view('user_list',$data);
		$this->load->view('inc_footer');	
	}
	
	/**
	 * 添加/编辑用户
	 * map to /users/edit/?
	 */
	public function edit()
	{
		$this->validate_login();
		
		$id = $this->uri->segment(3,0);
		
		$this->lang->load('user');
		$data = array('lang'=> $this->lang->language);
		
		$this->load->model('m_User');
		if($id){			
			$user = $this->m_User->select_one(array('user_id'=>$id));
			$data['user'] = $user;
		}
		else
			$data['user'] = new m_User();
				
		$this->load->view('inc_header',$data);
		$this->load->view('user_edit',$data);
		$this->load->view('inc_footer');
	}
	
	/*
	 * 用户编辑表单提交
 	 * map to /users/save
	 */
	public function save()
	{
		$this->validate_login();		
		//载入类库
		$this->load->model('v_Message');
		$this->load->model('m_User');
		$this->lang->load('user');
		
		//载入表单数据
		$user_id = $this->input->post('uid') * 1;
		$username = $this->input->post('username',true);
		$password = $this->input->post('password',true);
		$email = $this->input->post('email',true);
		$mobile = $this->input->post('mobile',true);
		
		$user = array(
			'username' => trim($username),
			'password' => md5(trim($password) .PWD_END_FIX),
			'email'	=>	$email,
			'mobile' =>	$mobile
		);
		//编辑
		if($user_id > 0){
			$user["user_id"] = $user_id;
			unset($user['username']);
			if(!trim($password)) unset($user['password']);
			$res = $this->m_User->update($user);
		}
		else{
		//添加
			if($this->m_User->exists($username)){
				$res = false;
				$this->v_Message->message = $this->lang->line('create_user_exists');				
			}
			else
				$res = $this->m_User->insert($user);
		}
		if($res){						
			$this->v_Message->message = $this->lang->line('create_user_success');	
			$this->v_Message->success = true;
			//写入日志
			$this->load->model('m_Log');
			$this->m_Log->write($this->current_user->user_id,$this->v_Message->message.$user_id, $this->current_user->username,'EDIT');
		}
		else{
			if(!$this->v_Message->message)
				$this->v_Message->message = $this->lang->line('create_user_fail');
		}
				
		//返回Json信息
		echo json_encode($this->v_Message);
	}
	
	/**
	 * 删除用户
	 * map to /users/del/?
	 */
	public function del()
	{
		$this->validate_login();
		
		$id = $this->uri->segment(3,0);
		
		$this->load->model('m_User');
		$this->load->model('v_Message');
		$this->lang->load('user');		
		if($id != $this->current_user->user_id){
			if($this->m_User->delete($id)){
				//写入日志
				$this->load->model('m_Log');
				$this->m_Log->write($this->current_user->user_id,$this->v_Message->message.$id, $this->current_user->username,'DELETE');
			}
		}
		else{
			//todo: throw an error message
		}
				
		redirect('/users');
	}
}